<?php
/**
 * 会员卡管理
 * @author tangbl
 *
 */
class VipcardAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 通过会员级别，获取自动生成的会员卡号
     * @access public
     * @param  Int    cardid 会员级别id
     * @return String 
     */
    public function getVipno(){
    	
    	$cardid = $this->_get("cardid","intval",0);
    	
    	if($cardid){
    		
    		$data = D("Vipcard")->getVipno($cardid);
    		
 			if($data){
 				$this->success($data);
 			}else{
 				$this->error("会员级别ID非法");
 			}
    	}else{
    		$this->error("会员级别ID必须输入");
    	}
    }
    
    /**
     * 获取会员卡级别
     * @access public
     * @param  Int company  商户id
     * @param return array
     */
    public function get(){
    	
    	$company = $this->_get("company","intval",0);
    	
    	$data = D("Vipcard")->get($company);
    	
    	$this->success($data);
    }
    
    /**
     * 重置会员卡密码
     * @access public
     * @param  Int company  商户id
     * @param  Int outletid 门店id
     * @param  Int customid 会员信息id
     * @param  String vipno 会员编号
     * @param  String password 会员卡密码
     * @return array
     */
    public function reset(){
    
    	$map['company']  = $this->_get("company","intval",0); 
    	$map['outletid'] = $this->_get("outletid","intval",0); 
    	$map['customid'] = $this->_get("customid","intval",0); 
    	$map['vipno']    = $this->_get("vipno","trim","");
    	$map['password'] = $this->_get("password","trim","");
    	$map['newpassword'] = $this->_get("newpassword","trim","");
    	
    	$data = D("VipUser")->reset($map);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 储值转账
     * @access public
     * @param  Int    company     商户id
     * @param  Int    outletid    门店id
     * @param  Int    customid    会员信息id
     * @param  double money       转账金额
     * @param  String vipno       转出会员卡号
     * @param  String input_vipno 转入会员卡号
     * @param  String password    会员卡密码
     * @return array
     */
    public function transferMoney(){
    
    	$map['company']     = $this->_get("company","trim","");
    	$map['outletid']    = $this->_get("outletid","trim","");
    	$map['customid']    = $this->_get("customid","trim","");
    	$map['money']       = $this->_get("money","trim","");
    	$map['vipno']       = $this->_get("out_vipno","trim","");
    	$map['input_vipno'] = $this->_get("input_vipno","trim","");
    	$map['password']    = $this->_get("password","trim","");
    	$map['remark']      = $this->_get("remark","trim","");
    	
    	$data = D("VipUser")->transferMoney($map);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 积分转账
     * @access public
     * @param  Int    company     商户id
     * @param  Int    outletid    门店id
     * @param  Int    customid    会员信息id
     * @param  double credit      转账积分
     * @param  String out_vipno   转出会员卡号
     * @param  String input_vipno 转入会员卡号
     * @param  String password    会员卡密码
     * @return array
     */
    public function transferCredit(){
    
    	$map['company']     = $this->_get("company","trim","");
    	$map['outletid']    = $this->_get("outletid","trim","");
    	$map['customid']    = $this->_get("customid","trim","");
    	$map['credit']      = $this->_get("credit","intval","");
    	$map['vipno']       = $this->_get("out_vipno","trim","");
    	$map['input_vipno'] = $this->_get("input_vipno","trim","");
    	$map['password']    = $this->_get("password","trim","");
    	 
    	$data = D("VipUser")->transferCredit($map);
    	 
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    
    }
    
    /**
     * 会员卡挂失(冻结)
     * @access public
     * @param  Int    company  商户id
     * @param  Int    outletid 门店id
     * @param  String $idStr   会员信息id（字符串类型，以英文逗号隔开）
     * @return array
     */
    public function frost(){
    	
    	$map['company']  = $this->_get("company","intval",0);
    	$map['outletid'] = $this->_get("outletid","intval",0);
    	
    	$idStr = $this->_get("id","trim",'');
    	
    	if($map){
    		
    		$idArr = explode(',',$idStr);
    		
    		foreach($idArr as $k=>$v){
    			$view = M("Custom")->find($v);
    			
    			$map['vipno'] = $view['vipno'];
    			$data         = D('VipUser')->frost($map);
    		}
    		
    		$this->success($data);
    	}else{
    		$this->error("参数非法，请重试");
    	}
    }
    
    /**
     * 会员卡挂失(解冻)
     * @access public
     * @param  Int    company  商户id
     * @param  Int    outletid 门店id
     * @param  String $idStr   会员信息id（字符串类型，以英文逗号隔开）
     * @return array
     */
    public function thaw(){
    	
    	$map['company']  = $this->_get("company","intval",0);
    	$map['outletid'] = $this->_get("outletid","intval",0);
    	 
    	$idStr = $this->_get("id","trim",'');
    	 
    	if($map){
    	
    		$idArr = explode(',',$idStr);
    	
    		foreach($idArr as $k=>$v){
    			$view = M("Custom")->find($v);
    			 
    			$map['vipno'] = $view['vipno'];
    			$data         = D('VipUser')->thaw($map);
    		}
    	
    		$this->success($data);
    	}else{
    		$this->error("参数非法，请重试");
    	}
    	
    }
    
    /**
     * 会员卡换卡
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     原会员卡号
     * @param  String new_vipno 新的会员卡号
     * @return array
     */
    public function changeCard(){

    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['customid'] = $this->_get("customid","trim","");
    	$map['vipno']    = $this->_get("vipno","trim","");
    	$new_vipno       = $this->_get("new_vipno","trim","");
    	
    	$data = D("VipUser")->changeCard($map,$new_vipno);
    	$data = D("Custom")->changeCard($map,$new_vipno);
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("会员卡换卡失败，请稍后重试");
    	}
    }
    
    /**
     * 验证会员卡号唯一性
     * @access public
     * @param  String vipno 会员卡号
     * @return array
     */
    public function checked(){
    
    	$vipno = $this->_req("vipno");
    	
    	$model = D('Vipcard');
    	$data  = $model->listAll();
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("暂无纪录！");
    	}

    	$data = D("VipUser")->checked($vipno);
    	
    	if($data){
    		$this->error("会员卡号已存在");
    	}else{
    		$this->success($data);
    	}
    }
    
    /**
     * 会员卡列表
     * retrun array
     */
    public function index(){
    	
    	$map['isdel'] = $this->_get("isdel","intval",2);
    	
    	$model = D('Vipcard');
    	$data  = $model->listAll($map);
    	
    	$this->success($data);
    }
    
    /**
     * 储值充值
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     会员卡号
     * @param  double money     充值金额
     * @param  String remark    备注信息
     * @return array
     */
    public function rechargeMoney(){

    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['customid'] = $this->_get("customid","trim","");
    	$map['vipno']    = $this->_get("vipno","trim","");
    	
    	$arr['use_money'] = $this->_get("money","trim","");
    	$arr['remark']    = $this->_get("remark","trim","");
    	
    	$data = D('VipUser')->rechargeMoney($map,$arr);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    	
    }
    
    /**
     * 储值消费
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     会员卡号
     * @param  double money     充值金额
     * @param  String remark    备注信息
     * @return array
     */
    public function consumptionMoney(){
    
    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['customid'] = $this->_get("customid","trim","");
    	$map['vipno']    = $this->_get("vipno","trim","");
    	
    	$arr['remark']      = $this->_get("remark","trim","");
    	$arr['password']    = $this->_get("password","trim","");
    	$arr['use_money']   = $this->_get("money","trim","");
    	 
    	$data = D('VipUser')->consumptionMoney($map,$arr);
    	 
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    	 
    }
    
    /**
     * 积分充值
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     会员卡号
     * @param  double money     充值积分
     * @param  String remark    备注信息
     * @return array
     */
    public function rechargeCredit(){

    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['customid'] = $this->_get("customid","trim","");
    	$map['vipno']    = $this->_get("vipno","trim","");
    	
    	$arr['remark']       = $this->_get("remark","trim","");
    	$arr['use_jifen']    = $this->_get("credit","trim","");
    	
    	$data = D('VipUser')->rechargeCredit($map,$arr);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    	
    }
    
    /**
     * 积分消费
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     会员卡号
     * @param  double credit    消费积分
     * @param  String remark    备注信息
     * @return array
     */
    public function consumptionCredit(){
    
    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['customid'] = $this->_get("customid","trim","");
    	$map['vipno']    = $this->_get("vipno","trim","");
    	
    	$arr['remark']      = $this->_get("remark","trim","");
    	$arr['password']    = $this->_get("password","trim","");
    	$arr['use_jifen']   = $this->_get("credit","trim","");
    	$arr['total_jifen'] = $this->_get("credit","trim","");
    	 
    	$data = D('VipUser')->consumptionCredit($map,$arr);
    	 
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    	 
    }
 	
    /**
     * 批量赠送积分
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    credit    要赠送的积分
     * @param  String remark    备注信息
     * @param  String val       选中的会员信息id
     * @return array
     */
    public function getIntegral(){

    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['credit']   = $this->_get("credit","intval",0);
    	$map['remark']   = $this->_get("remark","trim",'');
    	$map['val']      = $this->_get("val");
    	
    	$data = D('VipUser')->getIntegral($map);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 批量赠送储值
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    money    要赠送的金额
     * @param  String remark    备注信息
     * @param  String val       选中的会员信息id
     * @return array
     */
    public function getMoney(){
    
    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['money']    = $this->_get("money","intval",0);
    	$map['remark']   = $this->_get("remark","trim",'');
    	$map['val']      = $this->_get("val");
    	 
    	$data = D('VipUser')->getMoney($map);
    	 
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 批量积分清零
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  String val       选中的会员信息id
     * @return array
     */
    public function clearIntegral(){

    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['remark']   = $this->_get("remark","trim","");
    	$map['val']      = $this->_get("val","trim","");
    	
    	$data = D('VipUser')->clearIntegral($map);
    	
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 批量储值清零
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  String val       选中的会员信息id
     * @return array
     */
    public function clearMoney(){
    
    	$map['company']  = $this->_get("company","trim","");
    	$map['outletid'] = $this->_get("outletid","trim","");
    	$map['remark']   = $this->_get("remark","trim","");
    	$map['val']      = $this->_get("val","trim","");
    	 
    	$data = D('VipUser')->clearMoney($map);
    	 
    	if($data['status']==1){
    		$this->success($data);
    	}else{
    		$this->error($data['info']);
    	}
    }
    
    /**
     * 获取积分操作记录
     * @access public
     * @param  Int customid 用户信息id
     * @return array
     */
    public function getCreditRecords(){

    	$company  = $this->_get("company","intval",0);
    	$outletid = $this->_get("outletid","intval",0);
    	$desc     = $this->_get("orderDirection","trim","desc");;
    	$order    = $this->_get("orderName","trim","addtime");
    	$pageNum  = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p","intval",1);
    	$customid = $this->_get("customid","intval",0);
    	
    	$map['company']  = $company;
    	$map['outletid'] = $outletid;
    	$map['customid'] = $customid;
    	
    	import('ORG.Util.Page');
    	$model = D("VipCredit");
    	 
    	$count = $model->where($map)->count();
    	$Page  = new Page($count,$pageNum);
    	$list  = $model->where($map)->order($order.' '.$desc)
    			->limit($Page->firstRow.','.$Page->listRows)->select();
    	
    	$data  = array("count"=>$count, "list"=>$list);

    	$this->success($data);
    }
    
    /**
     * 获取金额操作记录
     * @access public
     * @param  Int customid 用户信息id
     * @return array
     */
    public function getMoneyRecords(){
    
    	$company  = $this->_get("company","intval",0);
    	$outletid = $this->_get("outletid","intval",0);
    	$desc     = $this->_get("orderDirection","trim","desc");;
    	$order    = $this->_get("orderName","trim","addtime");
    	$pageNum  = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p","intval",1);
    	$customid = $this->_get("customid","intval",0);
    	 
    	$map['company']  = $company;
    	$map['outletid'] = $outletid;
    	$map['customid'] = $customid;
    	 
    	import('ORG.Util.Page');
    	$model = D("VipMoney");
    
    	$count = $model->where($map)->count();
    	$Page  = new Page($count,$pageNum);
    	$list  = $model->where($map)->order($order.' '.$desc)
    	->limit($Page->firstRow.','.$Page->listRows)->select();
    	 
    	$data  = array("count"=>$count, "list"=>$list);
    
    	$this->success($data);
    } 
    
    
    
    
    
    
    
}